import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const state = {
  token: getToken(),
  name: '',
  avatar: '',
  user: {
    userType: '', // 用户类型
    userId: '', // 用户id
    userName: '', // 用户名称
    mobilePhone: '', // 用户手机号
    superAdmin: '', //  是否为超级 管理员
    permissions: [], // 权限数组
    currentPath: '', // 当前的路由
    orgList: [], // 机构列表
    orgId: '', // 当前选中机构id
    fileUrlPrefix: ''
  }
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_USER: (state, res) => {
    let data = res.data
    // 匹配路由权限
    state.user.orgId = data.orgId
    state.user.userType = data.userType // 用户类型
    state.user.userId = data.userId // 用户Id
    state.user.userName = data.userName // 用户姓名
    state.user.mobilePhone = data.mobilePhone // 用户手机号
    state.user.superAdmin = data.superAdmin // 是否为超级管理员
    state.user.permissions = data.permissions // 权限数组
    state.user.fileUrlPrefix = res.fileUrlPrefix // 权限数组
    localStorage.setItem('user_pm', JSON.stringify(state.user))
  },
  DEL_USER: (state) => {
    state.user = {
      userType: '', // 用户类型
      userId: '', // 用户id
      userName: '', // 用户名称
      mobilePhone: '', // 用户手机号
      superAdmin: false // 是否为超级管理员
    }
    localStorage.removeItem('user_pm')
    localStorage.removeItem('router_pm')
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        commit('SET_NAME', data.userName)
        commit('SET_USER', response)
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response

        if (!data) {
          reject('Verification failed, please Login again.')
        }

        const { name, avatar } = data

        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        commit('SET_TOKEN', '')
        commit('DEL_USER')
        removeToken()
        resetRouter()
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      commit('DEL_USER')
      removeToken()
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

